package com.zlm.zy.zhoujuanjuan.a;

import android.content.Context;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.zip.ZipFile;

/**
 * 创建时间: 2018/09/05 15:00
 * 作者: zhoujuanjuan
 * 描述: 读取写入comment操作
 */
public class CommentUtil {

  /**
   * 写入comment
   */
  public static void setZipComment(String filePath, String comment) {
    File file = new File(filePath);
    ZipFile zipFile = null;
    ByteArrayOutputStream outputStream = null;
    RandomAccessFile accessFile = null;
    try {
      zipFile = new ZipFile(file);
      outputStream = new ByteArrayOutputStream();

      byte[] byteComment = comment.getBytes();
      outputStream.write(byteComment);
      outputStream.write(short2Stream((short) byteComment.length));

      byte[] data = outputStream.toByteArray();

      accessFile = new RandomAccessFile(file, "rw");
      accessFile.seek(file.length() - 2);
      accessFile.write(short2Stream((short) data.length));
      accessFile.write(data);
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (zipFile != null) {
          zipFile.close();
        }
        if (outputStream != null) {
          outputStream.close();
        }
        if (accessFile != null) {
          accessFile.close();
        }
      } catch (Exception e) {

      }
    }
  }

  public static byte[] short2Stream(short data) {
    ByteBuffer buffer = ByteBuffer.allocate(2);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.putShort(data);
    buffer.flip();
    return buffer.array();
  }

  /**
   * 获取apk包路径
   * @param context
   * @return
   */
  public static String getPackagePath(Context context) {
    if (context != null) {
      return context.getPackageCodePath();
    }
    return null;
  }

  /**
   * 读取comment操作
   * @param filePath
   * @return
   */
  public static String getZipComment(String filePath) {
    File file = new File(filePath);
    byte[] bytes;
    RandomAccessFile randomAccessFile = null;
    try {
      randomAccessFile = new RandomAccessFile(file, "r");
      long index = randomAccessFile.length();

      bytes = new byte[2];
      index = index - bytes.length;
      randomAccessFile.seek(index);
      randomAccessFile.readFully(bytes);

      bytes = new byte[stream2Short(bytes, 0)];
      index = index - bytes.length;
      randomAccessFile.seek(index);
      randomAccessFile.readFully(bytes);

      return new String(bytes, "utf-8");
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (randomAccessFile != null) {
        try {
          randomAccessFile.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return "";
  }


  public static short stream2Short(byte[] stream, int offset) {
    ByteBuffer buffer = ByteBuffer.allocate(2);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.put(stream[offset]);
    buffer.put(stream[offset + 1]);
    return buffer.getShort(0);
  }
}
